package com.ia.aistream.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/***
 * quartz 中定时任务线程池配置
 */
@Configuration
public class ScheduleConfiger implements SchedulingConfigurer {


    private ThreadPoolTaskExecutor executor;
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        //设定一个长度50的定时任务线程池
        taskRegistrar.setScheduler(executor);
    }
//    private static final AtomicInteger POOL_NUMBER = new AtomicInteger(1);

//    @Override
//    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
//        //设定一个长度200的定时任务线程池
//        ThreadFactory factory = new TaskThreadFactory();
//        taskRegistrar.setScheduler(new ScheduledThreadPoolExecutor(50, factory));
//    }
//
//    private static class TaskThreadFactory implements ThreadFactory {
//        private final ThreadGroup group;
//        private final AtomicInteger threadNumber = new AtomicInteger(1);
//        private final String namePrefix;
//
//        private TaskThreadFactory() {
//            SecurityManager s = System.getSecurityManager();
//            group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
//            namePrefix = new StringBuilder().append("commander-sch-").append(POOL_NUMBER.getAndIncrement()).append("-task-").toString();
//        }
//
//        @Override
//        public Thread newThread(Runnable runnable) {
//            Thread thread = new Thread(group, runnable, namePrefix + threadNumber.getAndIncrement(), 0);
//            if (thread.isDaemon()) {
//                thread.setDaemon(false);
//            }
//            if (thread.getPriority() != Thread.NORM_PRIORITY) {
//                thread.setPriority(Thread.NORM_PRIORITY);
//            }
//            return thread;
//        }
//    }
}
